#include <iostream>
#include <algorithm>

using namespace std;

const int MAXM = 1000;
int n = 5, m = 7; // |V|=n, |E|=m
int u[MAXM] = {0,1,1,2,3,0,2};
int v[MAXM] = {1,3,4,3,4,2,4};
int w[MAXM] = {1,3,4,10,6,5,2};
int r[MAXM], p[MAXM];

bool cmp(const int i, const int j) {
    return w[i] < w[j];
}

int find(int x) {
    return p[x] == x ? x : p[x] = find(p[x]);
}

int Kruskal() {
    int ret = 0;
    for (int i = 0; i < n; ++i) p[i] = i;
    for (int i = 0; i < m; ++i) r[i] = i;
    sort(r, r + m, cmp);
    for (int i = 0; i < m; ++i) {
        int e = r[i]; int x = find(u[e]); int y = find(v[e]);
        if (x != y) { ret += w[e]; p[x] = y; }
    }
    return ret;
}

int main() {
    cout << Kruskal() << endl;
}
